package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseCategory3;
import com.atguigu.gmall.product.service.BaseCategory3Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/category3")
public class BaseCategory3Controller {
    @Autowired
    private BaseCategory3Service baseCategory3Service;
    /**
     * 根据id查询
     * @param id
     * @return®
     */
    @GetMapping(value = "/findById/{id}")
    public Result<BaseCategory3> findById(@PathVariable(value = "id")Long id){
        BaseCategory3 category3 = baseCategory3Service.getById(id);
        return Result.ok(category3);
    }

    /**
     * 查询所有的一级分类
     * @return
     */
    @GetMapping(value = "/findAll")
    public Result<List<BaseCategory3>> findAll(){
        List<BaseCategory3> baseCategory3s = baseCategory3Service.list(null);
        return Result.ok(baseCategory3s);
    }

    /**
     * 新增
     * @param baseCategory3
     * @return
     */
    @PostMapping(value = "/add")
    public Result add(@RequestBody BaseCategory3 baseCategory3){
        baseCategory3Service.save(baseCategory3);
        return Result.ok();
    }
    /**
     * 修改
     * @param baseCategory3
     * @return
     */
    @PutMapping(value = "/update")
    public Result update(@RequestBody BaseCategory3 baseCategory3){
        baseCategory3Service.updateById(baseCategory3);
        return Result.ok();
    }
    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete/{id}")
    public Result delete(@PathVariable(value = "id")Long id){
        baseCategory3Service.removeById(id);
        return  Result.ok();
    }

    /**
     * 条件查询
     * @param baseCategory3
     * @return
     */
    @PostMapping(value = "/search")
    public Result<List<BaseCategory3>> search(@RequestBody BaseCategory3 baseCategory3){
        return Result.ok(baseCategory3Service.search(baseCategory3));
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/getPage/{page}/{size}")
    public Result<List<BaseCategory3>> getPage(@PathVariable(value = "page")Integer page,
                                               @PathVariable(value = "size")Integer size){
        return Result.ok(baseCategory3Service.pageSearch(page,size));
    }

    /**
     * 分页条件查询
     * @param baseCategory3
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}")
    public Result<List<BaseCategory3>> search(@RequestBody BaseCategory3 baseCategory3,
                                              @PathVariable(value = "page")Integer page,
                                              @PathVariable(value = "size")Integer size){
        return Result.ok(baseCategory3Service.search(baseCategory3,page,size));
    }
}
